package driving;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.g2room.driving.model.po.Article;
import com.g2room.driving.util.DbUtil;

public class HandleUnivContentByJsonup {
	private static final int type_qg = 1;
	private static final int type_kc = 2;
	static Map<String, String> oldImgMap = null;//getOldImgs();
	public static void main(String[] args) {
		handlUnivArti();
	}

	
	public static void handlUnivArti() {
		//getDetails(String condition, String source, int type)
		List<Article> articles = getDetails("pagenav", null, type_qg);
		System.out.println("总文章："+articles.size());
		int i = 0;
		for(Article arti : articles) {
			try{
	            //实例化一个本地html文件的Parser
				String html = arti.getContent();
				Document doc = Jsoup.parse(html);
				doc.getElementById("pagenav").remove();
				doc.getElementsByAttributeValue("class", "page_610").remove();
				doc.getElementsByAttributeValue("class", "wx-660").remove();
				Elements tableEls = doc.getElementsByTag("table");
				if(null != tableEls) {
					for(Element table : tableEls) {
						String tableHtml = table.html();
						if(tableHtml.contains("高考真题") && tableHtml.contains("2016高考查分")) {
							table.remove();
						}
					}
				}
				i++;
	            updateContent(arti.getId(), handleSpace(doc.body().html()));
	        }catch(Exception e) {
	        	System.out.println("id========" + arti.getId());
	        	e.printStackTrace();
	        }
		}
    	System.out.println("共处理数据：" + i);
	}
	
	public static String handleSpace(String str) {
		return str.replaceAll("&nbsp;", " ");
	}
	
	public static void updateContent(long id, String content) {
		update(id, content, "content", type_qg);
	}
	

	public static void update(long id, String text, String t, int type) {
		try {
			Connection connection = DbUtil.getConn();
			PreparedStatement statement = connection.prepareStatement("update ncee_arti set "+t+" =?,source='content' where id=?");
			statement.setString(1, text);
			statement.setLong(2, id);
			statement.execute();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static List<Article> getDetails(String condition, String source) {
		return getDetails(condition, source, type_kc);
	}

	public static List<Article> getDetails(String condition, String source, int type) {
		List<Article> articles = new ArrayList<Article>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			condition = (null == condition) ? condition = "" : "and content like '%"+condition+"%' "; 
			source = (null == source) ? source = "" : "and source='"+source+"' ";
//			String sql = "select id,content from d_article_grap "
//			String sql="select id,category_id, category_code, type_code, type_id,title,content from d_article_grap "
			String sql="select id,content,hot from ncee_arti"
//					+" where hot = 2"
//			String sql = "select id,content from d_article "
					+ " where 1=1 " + condition + source;
//					+ "where id in (574)"
//					+ "where id=6632 limit 1";
					;
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
//				int hot = rs.getInt("hot");
//				if(hot == 1) continue;
				Article article = new Article();
				article.setId(rs.getLong("id"));
				article.setContent(rs.getString("content"));
//				article.setCategoryCode(rs.getString("category_code"));
//				article.setTypeCode(rs.getString("type_code"));
//				if(null == article.getTypeCode() || article.getTypeCode().length()==0) {
//					article.setTypeCode(article.getCategoryCode());
//				}
				articles.add(article);
			}
			rs.close();
			statement.close();
//			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
	
}
